﻿using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IServices.ProductProcess;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.ProductProcess
{
    public class ProProcessServices:IProProcess
    {
        public IBaseRepository Db { get; }

        public ProProcessServices(IBaseRepository db)
        {
            Db = db;
        }

        public async Task<IEnumerable<ProProcess>> GetAllAsync()
        {
            string sql = "select * from ProProcess";
            var list = await Db.QueryAllAsync<ProProcess>(sql);
            return list;
        }
        public async Task<IEnumerable<ProProcess>> GetAsync(int PageIndex, int PageSize, string ProcessCode, string ProcessName, string EnableFlag)
        {
            string sql = "select * from ProProcess where 1=1 ";

            if (!string.IsNullOrEmpty(ProcessCode))
            {
                sql += " and ProcessCode like @ProcessCode ";
            }
            if (!string.IsNullOrEmpty(ProcessName))
            {
                sql += " and ProcessName like @ProcessName ";
            }
            if (!string.IsNullOrEmpty(EnableFlag.ToString()))
            {
                sql += " and EnableFlag like @EnableFlag ";
            }
            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only ";
            var list = await Db.QueryAsync<ProProcess>(sql, new { PageIndex, PageSize, ProcessCode = $"%{ProcessCode}%" , ProcessName = $"%{ProcessName}%" , EnableFlag = $"%{EnableFlag}%" });
            return list;
        }

        public async Task<int> GetCountAsync(string ProcessCode, string ProcessName, string EnableFlag)
        {
            string sql = "select count(*) from ProProcess where 1=1 ";


            if (!string.IsNullOrEmpty(ProcessCode))
            {
                sql += " and ProcessCode like @ProcessCode ";
            }

            if (!string.IsNullOrEmpty(ProcessName))
            {
                sql += " and ProcessName like @ProcessName ";
            }

            if (!string.IsNullOrEmpty(EnableFlag.ToString()))
            {
                sql += " and Attention like @EnableFlag ";
            }

            var count = await Db.GetRecoreCountAsync(sql, new { ProcessCode = $"%{ProcessCode}%", ProcessName = $"%{ProcessName}%", EnableFlag = $"%{EnableFlag}%" });
            return count;
        }

        public async Task<bool> UpdateAsync(ProProcess model)
        {
            var result = await Db.UpdateAsync(model);
            return result;
        }

        public async Task<bool> AddAsync(ProProcess model)
        {

            var result = await Db.InsertAsync(model);
            return result;
        }

        public async Task<bool> DeleteAsync(int[] Ids)
        {

            string sql = "delete ProProcess where Id in @Ids";
            var result = await Db.DeleteAsync(sql, new { Ids });
            return result;
        }

        public async Task<bool> DeleteAsync(int Id)
        {
            string sql = "delete ProProcess where Id = @Id";
            var result = await Db.DeleteAsync(sql, new { Id });
            return result;
        }
    }
}
